//package com.zlp.mall.portal.interceptor;
//
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.json.JSONUtil;
//import com.zlp.mall.common.api.R;
//import com.zlp.mall.common.api.RespCode;
//import com.zlp.mall.common.util.JwtTokenUtil;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.util.PathMatcher;
//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.List;
//
///**
// * 作用： 验证 用户是否登录、菜单资源权限
// * @author ZouLiPing
// */
//@Slf4j(topic = "AuthInterceptor")
//public class AuthInterceptor extends HandlerInterceptorAdapter {
//
//    public static ThreadLocal<String> threadLocalUserName = new ThreadLocal<>();
//    // 配置文件中的白名单secure.ignored.urls
//    private List<String> urls;
//    @Value("${jwt.tokenHead}")
//    private String tokenHead;
//    @Resource
//    private JwtTokenUtil jwtTokenUtil;
//
//    @SneakyThrows
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  {
//        //1、不需要登录就可以访问的路径——白名单
//        // 获取当前请求   /admin/login
//        String requestURI = request.getRequestURI();
//        // Ant方式路径匹配 /**  ？  _
//        PathMatcher matcher = new AntPathMatcher();
//        for (String ignoredUrl : urls) {
//            if(matcher.match(ignoredUrl,requestURI)){
//                return  true;
//            }
//        }
//        String authorization = request.getHeader("Authorization");
//        if(StrUtil.isNotBlank(authorization)){
//            log.info("authorization={}",authorization);
//            if (!authorization.startsWith(tokenHead)) {
//                returnFalse(response);
//            }
//            String token = authorization.substring(tokenHead.length() + 1);
//            String userName = jwtTokenUtil.getUserNameFromToken(token);
//            if (StrUtil.isBlank(userName)) {
//                returnFalse(response);
//            }
//            threadLocalUserName.set(userName);
//            return  true;
//        }
//        return  false;
//    }
//
//    private boolean returnFalse(HttpServletResponse response) throws IOException {
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Cache-Control","no-cache");
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json");
//        response.getWriter().println(JSONUtil.parse(R.unauthorized(RespCode.UNAUTHORIZED.getMessage())));
//        response.getWriter().flush();
//        return  false;
//    }
//
//
//    public List<String> getUrls() {
//        return urls;
//    }
//
//    public void setUrls(List<String> urls) {
//        this.urls = urls;
//    }
//
//    public static String getUserName(){
//
//        return threadLocalUserName.get();
//
//    }
//}
